require("os")
require("lfs")
require("curl")
require("zip")

-- Download the file given by the locator to the
-- given d-estination.  Really simple-minded,
-- reads the all located data into memory before
-- writing it all at once.

local function download(locator, d)
	local function mkwcb(t)
		return function(s, len)
			print(len, "bytes")
			table.insert(t, s)
			return len,nil
		end
	end

	print("Downloading " .. locator)

	local dt = {}
	local c = curl.easy_init()
	if c then
		c:setopt(curl.OPT_URL, locator)
		c:setopt(curl.OPT_WRITEFUNCTION,mkwcb(dt))
		local r, e = c:perform()
		if r == 0 then
			local oot = io.open(d, 'wb')
			if oot then
				oot:write(table.concat(dt))
				oot:close()
			else
				print("Cannot open output file " .. d)
			end
		else
			print("Error in curl.perform(): " .. e)
		end
	else
		print("Cannot easy curl.easy_init().")
	end

	print("Finished downloading " .. d)
end

-- from the given zfile, s-ource "extract to" d-estination.

local function exto(zfile,s,d)
	print(s,d)
	local f = zfile:open(s)
	if f then
		local a = f:read("*a")
		f:close()
		local oot = io.open(d, 'wb')
		if oot then
			oot:write(a)
			oot:close()
		else
			print("Cannot open output file " .. d)
		end
	else
		print("Cannot open input file " .. s)
	end
end

-- install NTLua

local function instnt(dl, dz)
	local dodl = true
	local delzip = true
	if dl == false then dodl = false end
	if dz == false then delzip = false end

	local dest = 'ntlua.zip'
	local loc =
'http://luaforge.net/frs/download.php/2539/ntlua3_Win32_dll_lib.zip'

	lfs.chdir(reaper.realua.luadir)
	if dodl == true then
		download(loc, dest)
	end

	local zfile = zip.open(dest)
	if zfile then
		exto(zfile, 'ntlua.dll', 'lib\\ntlua.dll')
		exto(zfile, 'cacls.dll', 'lib\\cacls.dll')
		zfile:close()

		if delzip == true then
			os.remove(dest)
		end

		if require("ntlua") then
			print("Successful Install of NTLua.")
		end
	else
		print("Cannot open downloaded file " .. dest)
	end
end

-- Install LuaXML

local function instxml(dl, dz)
	local dodl = true
	local delzip = true
	if dl == false then dodl = false end
	if dz == false then delzip = false end

	lfs.chdir(reaper.realua.luadir)

	local dest = "xml.zip"
	local loc =
"http://www.viremo.de/LuaXML/LuaXML_090330.zip"

	if dodl == true then
		download(loc, dest)
	end

	local zfile = zip.open(dest)
	if zfile then
		exto(zfile, "LuaXML_lib.dll", "lib\\LuaXML_lib.dll")
		exto(zfile, "LuaXml.lua", "lib\\LuaXml.lua")

		zfile:close()
		if delzip == true then
			os.remove(dest)
		end

		if require("LuaXml") then
			print("Successful Install of LuaXML.")
		end
	else
		print("Cannot open downloaded file " .. dest)
	end
end

local function extdirex(zf, finddir, findext, dstdir)
	for file in zf:files() do
		local d, f, e = splitfname(file.filename)
		if d == finddir and e == findext then
--			print(file.filename, dstdir .. f .. e)
			exto(zf, file.filename, dstdir .. f .. e)
		end
	end
end

local function extdir(zf, finddir, dstdir)
	for file in zf:files() do
		local d, f, e = splitfname(file.filename)
		if d == finddir and f ~= "" then
--			print(file.filename, dstdir .. f .. e)
			exto(zf, file.filename, dstdir .. f .. e)
		end
	end
end

-- Install wxLua

local function instwx(dl, dz)
	local dodl = true
	local delzip = true
	if dl == false then dodl = false end
	if dz == false then delzip = false end

	print("wxLua is big so this may take a long time!")

	lfs.chdir(reaper.realua.luadir)

	local dest = "wx.zip"
	local loc = "http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/w/project/wx/wxlua/wxlua/2.8.10.0/wxLua-2.8.10-MSW-dll.zip"

	if dodl == true then
		download(loc, dest)
	end

	reaper.sleep()
	print("Extracting from " .. dest)

	local zfile = zip.open(dest)
	if zfile then
		lfs.mkdir("lib\\wx")
		lfs.mkdir("lib\\wx\\docs")
		lfs.mkdir("lib\\wx\\samples")
		lfs.mkdir("lib\\wx\\samples\\image")

		local bindir = "wxLua-2.8.10-MSW-dll/bin/"
		local docdir = "wxLua-2.8.10-MSW-dll/docs/"
		local samdir = "wxLua-2.8.10-MSW-dll/samples/"
		local imgdir = "wxLua-2.8.10-MSW-dll/samples/image/"

		extdirex(zfile, bindir, ".dll", "lib\\wx\\")
		extdir(zfile, docdir, "lib\\wx\\docs\\")
		extdir(zfile, samdir, "lib\\wx\\samples\\")
		extdir(zfile, imgdir, "lib\\wx\\samples\\image\\")

		zfile:close()

		os.remove("lib\\wx\\lua5.1.dll")

		if delzip == true then
			os.remove(dest)
		end

		local dubya = require("wx")
		if wx then
			print("Successful Install of wxLua!")
		else
			print("require('wx') no workie returning ", dubya)
		end
	else
		print("Cannot open downloaded file " .. dest)
	end
end

local function instmd5(dl, dz)
	local dodl = true
	local delzip = true
	if dl == false then dodl = false end
	if dz == false then delzip = false end

	lfs.chdir(reaper.realua.luadir)
	lfs.mkdir("lib\\md5")

	local dest = 'md5.zip'
	local loc =
'http://luaforge.net/frs/download.php/3357/md5-1.1.2-win32-lua51.zip'

	if dodl == true then
		download(loc, dest)
	end

	local zfile = zip.open(dest)
	if zfile then
		exto(zfile, 'md5-1.1.2-win32-lua51/lib/md5/core.dll', 'lib\\md5\\core.dll')
		exto(zfile, 'md5-1.1.2-win32-lua51/lib/des56.dll', 'lib\\des56.dll')
		exto(zfile, 'md5-1.1.2-win32-lua51/lua/md5.lua', 'lib\\md5.lua')
		zfile:close()

		if delzip == true then
			os.remove(dest)
		end

		if require("md5") then
			print("Successful Install of md5.")
		end
		if require("des56") then
			print("Successful Install of des56.")
		end
	else
		print("Cannot open downloaded file " .. dest)
	end
end


-- instnt()
-- instxml()
-- instmd5()
instwx()


